import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';

import '../models/res_partner.dart';

extension String2DataExt on String? {
  String? get getValue => this == null || this == 'false' ? '' : this;

  bool checkValue(String value) => this == value;

  bool checkNotValue(String? value) => value != null && this != value;

  Color get color {
    if (this == null) {
      return Colors.transparent;
    }
    try {
      if (this!.startsWith('#')) {
        var temp = this!.replaceAll('#', '');
        return Color(int.parse('FF$temp', radix: 16));
      } else {
        var temp = this!.replaceAll('rgb(', '');
        temp = temp.replaceAll(')', '');
        var rgb = temp.split(',').map((e) => int.parse(e)).toList();
        return Color.fromARGB(255, rgb[0], rgb[1], rgb[2]);
      }
    } catch(_) {
      return const Color(0xFF22CE67);
    }
  }

  Uint8List? get bytes => this == null || getValue!.isEmpty ? null : base64Decode(this!);
}

extension Num2DataExt on num? {
  String get fixed => this?.toStringAsFixed(2) ?? '0';

  String get string2 => (this ?? 0) > 10 ? '$this' : '0$this';

  String get fixed3 {
    var temp = fixed;
    var index = temp.indexOf('.');
    if (index == 1) {
      return '00$temp';
    } else if (index == 2) {
      return '0$temp';
    } else {
      return temp;
    }
  }
}

extension StringList2DataExt on List<String>? {
  String? get firstValue => this?.isEmpty ?? true ? null : this!.first;
}

extension ResPartner2DataExt on ResPartner? {
  String get mergeAddress =>
      this == null ? '' : '${this!.street?.getValue ?? ''} ${this!.city?.getValue ?? ''} ${this!.zip?.getValue ?? ''}';
}
